package com.resolution.dietaryassessment.view.chronic.model;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.resolution.dietaryassessment.R;

import java.util.List;

public abstract class ParentAdapter<T> extends RecyclerView.Adapter{

    protected List<T> mDataSource;
    private Context mContext;
    private int resID = R.layout.list_item;

    public ParentAdapter(List<T> mDataSource, Context mContext) {
        this.mDataSource = mDataSource;
        this.mContext = mContext;
    }

    public ParentAdapter(List<T> mDataSource, Context mContext, int resID) {
        this.mDataSource = mDataSource;
        this.mContext = mContext;
        this.resID = resID;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new CommonViewHolder(LayoutInflater.from(mContext).inflate(resID, null));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        CommonViewHolder holder = (CommonViewHolder)viewHolder;
        if (resID == R.layout.list_item){
            ProgressPointView mProgressPointView = holder.getView(R.id.tav_line);
            if (position == 0){
                mProgressPointView.isHeadView(true);
            }else{
                mProgressPointView.isHeadView(false);
            }
            if (position == mDataSource.size()-1){
                mProgressPointView.isFootView(true);
            }else{
                mProgressPointView.isFootView(false);
            }
        }
        initView(holder, position);
    }

    protected abstract void initView(CommonViewHolder holder, int position);

    @Override
    public int getItemCount() {
        return mDataSource == null ? 0 : mDataSource.size();
    }
}
